#ifndef __SYNCOBJECT_H__
#define __SYNCOBJECT_H__

class CCritialSectionLock
{
public:
	CCritialSectionLock(LPCRITICAL_SECTION pCs);
	~CCritialSectionLock();
private:
	void Lock();
	void Unlock();
	LPCRITICAL_SECTION m_pCs;
};

inline void CCritialSectionLock::Lock()
{ ASSERT(m_pCs); 
//TRACE(_T("Enter %x\r\n"),m_pCs);
::EnterCriticalSection(m_pCs); 
}

inline void CCritialSectionLock::Unlock()
{ ASSERT(m_pCs); 
//TRACE(_T("Leave %x\r\n"),m_pCs);
::LeaveCriticalSection(m_pCs); }

inline CCritialSectionLock::~CCritialSectionLock()
{ Unlock(); }

class CSleepObject
{
public:
	CSleepObject();
	~CSleepObject();
	void Init();
	BOOL Lock();
	BOOL Unlock();
private:
	long volatile m_lLockCount;
};

class CSleepLock
{
public:
	CSleepLock(CSleepObject *pSObj);
	~CSleepLock();

private:
	CSleepObject *m_pSObj;
};

inline CSleepLock::CSleepLock(CSleepObject *pSObj)
{
	ASSERT(pSObj);
	m_pSObj = pSObj;
	m_pSObj->Lock();
}

inline CSleepLock::~CSleepLock()
{
	ASSERT(m_pSObj);
	m_pSObj->Unlock();
	m_pSObj = NULL;
}

#endif //__SYNCOBJECT_H__